package com.ruyuan.user.server.domain.converter;

import com.ruyuan.common.model.Features;
import com.ruyuan.user.api.model.enums.OrgTypeEnum;
import com.ruyuan.user.server.domain.model.Organization;
import com.ruyuan.user.server.repository.bean.OrganizationDO;
import com.ruyuan.user.server.repository.mapper.OrganizationMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * @author xx
 */
@Component
public class OrganizationConverter {

    @Autowired
    private OrganizationMapper mapper;

    public OrganizationDO convertModelToDO(Organization organization) {
        if (organization == null) {
            return null;
        }
        OrganizationDO organizationDO = new OrganizationDO();
        BeanUtils.copyProperties(organization, organizationDO);
        if (!organization.getFeatures().isEmpty()) {
            organizationDO.setFeatures(organization.getFeatures().toString());
        }
        if (organization.getOrgType() != null) {
            organizationDO.setOrgType(organization.getOrgType().getCode());
        }
        if (!CollectionUtils.isEmpty(organization.getOrgPath())) {
            organizationDO.setOrgPath(organization.getOrgPath().stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        if (organization.getId() == null) {
            organizationDO.setCreator(organization.getModifier());
            organizationDO.setGmtCreate(new Date());
            organizationDO.setIsDeleted("n");
        } else {
            OrganizationDO record = mapper.selectByPrimaryKey(organization.getId());
            organizationDO.setCreator(record.getCreator());
            organizationDO.setGmtCreate(record.getGmtCreate());
            if (organization.getIsDeleted() != null && organization.getIsDeleted()) {
                organizationDO.setIsDeleted("y");
            } else {
                organizationDO.setIsDeleted(record.getIsDeleted());
            }
        }
        organizationDO.setGmtModified(new Date());
        return organizationDO;
    }

    public Organization convertDOToModel(OrganizationDO organizationDO) {
        if (organizationDO == null) {
            return null;
        }
        Organization organization = new Organization();
        BeanUtils.copyProperties(organizationDO, organization);
        if (StringUtils.isNotBlank(organizationDO.getFeatures())) {
            organization.setFeatures(Features.of(organizationDO.getFeatures()));
        }
        if (StringUtils.isNotBlank(organizationDO.getOrgType())) {
            organization.setOrgType(OrgTypeEnum.getByCode(organizationDO.getOrgType()));
        }
        if (StringUtils.isNotBlank(organizationDO.getOrgPath())) {
            organization.setOrgPath(Arrays.stream(organizationDO.getOrgPath().split(",")).map(Long::parseLong).collect(Collectors.toList()));
        } else {
            organization.setOrgPath(new ArrayList<>());
        }
        return organization;
    }

}
